package com.bgybmi.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSON;
import com.bgybmi.config.ConfigEnum;
import com.bgybmi.entity.BxDevice;
import com.bgybmi.entity.CoMeetOrder;
import com.bgybmi.entity.JbRoom;
import com.bgybmi.entity.MeetCreate;
import com.bgybmi.entity.Users;
import com.bgybmi.sender.Sender;
import com.bgybmi.sender.WeChatFactory;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordException;
import com.jfinal.plugin.activerecord.Db;

public class MeetService {

	public static final MeetService me = new MeetService();

	// insert

	public boolean submit(String room, String topic, String zcr, String starttime, String finishtime, String chr,
			String prechr, String bz, String ydr, String device, String isprivate, String isremind,String isdrink,String url) {
		MeetCreate mc = new MeetCreate();
		boolean bl=false;
		//mc.find("select * from mms_create");
//		boolean bl = mc.save(room, topic, zcr, starttime, finishtime, chr, prechr, bz, ydr, device, isprivate,
//				isremind);
		mc.set("room", room);
		mc.set("topic",topic);
		mc.set("starttime", starttime);	
		mc.set("finishtime",finishtime);	
		System.out.println(finishtime);//null
		mc.set("zcr", zcr);	
		mc.set("chr", chr);
		mc.set("prechr", prechr);
		mc.set("meetbz",bz);
		mc.set("ydr", ydr);
		mc.set("device",device);
		mc.set("issend", 0);
		mc.set("status", 0);
		mc.set("isprivate", isprivate);
		mc.set("isremind", isremind);
		mc.set("isdrink", isdrink);
		boolean result = false;
		try{
			 bl = mc.save();	
		}catch(ActiveRecordException E){
			E.printStackTrace();
		}
		if(bl==true){
			url=url+"&xh="+mc.get("xh");
			// + "meetingorder?roomname="+room;//这里的路径据说他们不是 别
			String msg = "您有新的会议将于" + starttime + "开始,会议主题" + topic + ",<a href=\"" + url + "\">查看</a>";
			System.out.println(msg);
			// sendTextMssage(chr.replace(",", "|"),msg);
			WeChatFactory wcf = new WeChatFactory(chr.replace(",", "|"), msg, PropKit.get(ConfigEnum.HY.getAgentId()));
			Sender pro = wcf.produce();
			pro.Send();
		}
		return bl;
	}
	
	
	public void drinkSave(String meetxh,String meis, String naik, String kaib,String nat, String xiangc, String zheng, String jiaot, String mok,
			String guoz) {
		// TODO Auto-generated method stub
		CoMeetOrder cmo=new CoMeetOrder();
		cmo.set("meetxh", meetxh);
		cmo.set("meis", meis);
		cmo.set("naik",naik);
		cmo.set("kaib",kaib);
		cmo.set("nat",nat);
		cmo.set("xiangc",xiangc);
		cmo.set("zheng",zheng);
		cmo.set("jiaot",jiaot);
		cmo.set("mok", mok);
		cmo.set("guoz",guoz);
		cmo.save();
	}
	
	//可变参数也可以
	public boolean submit(String room, String topic, String zcr, String starttime, String finishtime, String chr,
			String prechr, String bz, String ydr, String device, String isprivate, String isremind,String isdrink,String url,
			String meis, String naik,String kaib,String nat, String xiangc, String zheng, String jiaot, String mok, String guoz) {
		// TODO Auto-generated method stub
		MeetCreate mc = new MeetCreate();
		boolean bl=false;
		//mc.find("select * from mms_create");
//		boolean bl = mc.save(room, topic, zcr, starttime, finishtime, chr, prechr, bz, ydr, device, isprivate,
//				isremind);
		mc.set("room", room);
		mc.set("topic",topic);
		mc.set("starttime", starttime);	
		mc.set("finishtime",finishtime);	
		System.out.println(finishtime);//null
		mc.set("zcr", zcr);	
		mc.set("chr", chr);
		mc.set("prechr", prechr);
		mc.set("meetbz",bz);
		mc.set("ydr", ydr);
		mc.set("device",device);
		mc.set("issend", 0);
		mc.set("status", 0);
		mc.set("isprivate", isprivate);
		mc.set("isremind", isremind);
		mc.set("isdrink", isdrink);
		boolean result = false;
		try{
			 bl = mc.save();	
			 String meetxh=mc.get("xh")+"";
			 drinkSave(meetxh,meis, naik,kaib,nat, xiangc, zheng, jiaot, mok, guoz);
			 
		}catch(ActiveRecordException E){
			E.printStackTrace();
		}
		if(bl==true){
			url=url+"&xh="+mc.get("xh");
			// + "meetingorder?roomname="+room;//这里的路径据说他们不是 别
			String msg = "您有新的会议将于" + starttime + "开始,会议主题" + topic + ",<a href=\"" + url + "\">查看</a>";
			System.out.println(msg);
			// sendTextMssage(chr.replace(",", "|"),msg);
			WeChatFactory wcf = new WeChatFactory(chr.replace(",", "|"), msg, PropKit.get(ConfigEnum.HY.getAgentId()));
			Sender pro = wcf.produce();
			pro.Send();
		}
		return bl;
	}
	
	/**
	 * @description 
	 * @param drink
	 */
	public void submit(String drink) {
		// TODO Auto-generated method stub
		JSON.parse(drink);		
	}
	
	public boolean create(String roomname, String bz, String userid) {
		// TODO Auto-generated method stub
		Users user= Users.me.findUserByUsername(userid);
		String groupid=user.get("groupid")+"";
		JbRoom jb = new JbRoom();
		boolean bs = jb.save(roomname, bz, userid,groupid);
		return bs;
	}
	
	
	// query
	/**
	 * @descripiton 获取饮品信息接口
	 * @param meetxh
	 * @return
	 */
	public List<CoMeetOrder> findByMeetXh(String xh){
		
		return CoMeetOrder.me.findByMeetXh(xh);		
	}
	
	public List<JbRoom> getAllBase() {
		return JbRoom.me.getAllBase();
	}

	public List<MeetCreate> getList(String userid) {
		// TODO Auto-generated method stub
		return MeetCreate.me.getList(userid);
	}

	public List<Users> findByUsername(String userid) {
		return Users.me.findByUsername(userid);
	}

	public List<BxDevice> listAllByParam(String string, String roomname) {
		// TODO Auto-generated method stub
		return BxDevice.me.listAllByParam("roomname", roomname);
	}

	public List<MeetCreate> getAllMeetTillNow() {
		// TODO Auto-generated method stub
		Date starttime = new Date();
		return MeetCreate.me.getAllMeetTillNow(starttime);
	}

	public Map getMeetByXh(String xh) {
		// TODO Auto-generated method stub
		MeetCreate mc = MeetCreate.me.getMeetByXh(xh);
		// 重构信息
		Map every = new HashMap();
		every.put("zcrname", userParse(mc.getStr("zcr")));
		every.put("chrname", userParse(mc.getStr("chr")));
		every.put("ydrname", userParse(mc.get("ydr")));
		every.put("mc", mc);
		return null;
	}

	public Map meetinfoQuery(String xh) {
		// TODO Auto-generated method stub
		MeetCreate mc = MeetCreate.me.getMeetByXh(xh);
		// 重构信息
		Map every = new HashMap();
		every.put("zcrname", userParse(mc.getStr("zcr")));
		every.put("chrname", userParse(mc.getStr("chr")));
		every.put("ydrname", userParse(mc.get("ydr")));
		every.put("mc", mc);
		return every;
	}

	/**
	 * message send
	 * 
	 * @return
	 */
	public String userParse(String users) {
		String user[] = StringUtils.split(users, ",");
		// .spusers.split(",");
		String temp[] = new String[user.length];// base Type Array
		for (int i = 0; i < user.length; i++) {
			//
			temp[i] = changeUser(user[i]);
		}
		return StringUtils.join(temp, ",");
	}

	public String changeUser(String user) {
		String restr = null;
		List<Users> fbu = Users.me.findByUsername(user);
		if (fbu.isEmpty()) {
			restr = user;
		} else {
			restr = fbu.get(0).get("username");
		}
		return restr;
	}

	public List<JbRoom> getAllCreate(String userid) {
		// TODO Auto-generated method stub
		Users u=Users.me.findUserByUsername(userid);
		return JbRoom.me.getAllCreate(userid,u.get("groupid")+"");
	}

	public List<JbRoom> getAllList(String userid) {
		// TODO Auto-generated method stub
		return JbRoom.me.getAllList(userid);
	}

	public List<JbRoom> listRoom(String roomname) {
		// TODO Auto-generated method stub
		return JbRoom.me.listRoom(roomname);
	}

	public List<MeetCreate> getAllByRoom(String room) {
		// TODO Auto-generated method stub
		MeetCreate mc = new MeetCreate();
		List<MeetCreate> abr = mc.me.getAllByRoom(room);
		return abr;
	}
	
	//update
	
	public boolean edit(String xh, String roomname, String bz) {
		// TODO Auto-generated method stub
		boolean editRes = false;
		JbRoom jb = JbRoom.me.findById(xh);
		try {
			jb.set("roomname", roomname);
			jb.set("bz", bz);
			jb.update();
			editRes = true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return editRes;
	}
	
	
	
	//delete
	public boolean delete(String xh) {
		// TODO Auto-generated method stub
		boolean deleteRes=false;
		JbRoom jb = JbRoom.me.findById(xh);
		try {
			jb.delete();
			deleteRes = true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return deleteRes;
	}

	public boolean update(String xh,String room, String topic, String starttime, String finishtime, String zcr, String chr,
			String prechr, String bz, String ydr, String device, String isprivate, String isremind, String url) {
		// TODO Auto-generated method stub
		boolean flag = false;
		MeetCreate mc = new MeetCreate().findById(xh);
		try {
			mc.set("room", room);
			mc.set("topic", topic);
			mc.set("starttime", starttime);
			mc.set("finishtime", finishtime);
			mc.set("zcr", zcr);
			mc.set("chr", chr);
			mc.set("prechr", prechr);
			mc.set("meetbz", bz);
			mc.set("ydr", ydr);
			mc.set("device", device);
			mc.set("issend", 0);
			mc.set("status", 0);
			mc.set("isprivate", isprivate);
			mc.set("isremind", isremind);
			mc.update();
			// 发送信息
			// + "meetingorder?roomname="+room;//这里的路径据说他们不是 别

			// + "meetingorder?roomname="+room;//这里的路径据说他们不是 别
			url=url+"&xh="+mc.get("xh");
			String msg = "会议修改:将于" + starttime + "开始,会议主题" + topic + ",<a href=\"" + url + "\">查看</a>";
			// sendTextMssage(chr.replace(",", "|"),msg);
			WeChatFactory wcf = new WeChatFactory(chr.replace(",", "|"), msg, PropKit.get(ConfigEnum.HY.getAgentId()));
			Sender pro = wcf.produce();
			pro.Send();
			//线程终止
			
			/*
			 * SendMsg sm=new SendMsg(chr.replace(",", "|"),msg); sm.start();
			 */
			flag = true;
		} catch (Exception e) {
			// TODO: handle exception
			throw new RuntimeException(e);
		}
		return flag;
	}

	public boolean deletemeet(String xh,String url) {
		// TODO Auto-generated method stub
		boolean deleteRes=false;
		MeetCreate mc = MeetCreate.me.findById(xh);
		try {
			mc.delete();
			//CoMeetOrder cmo= CoMeetOrder.me.findByMeetXh(xh).get(0);
			CoMeetOrder cmo=new CoMeetOrder();
			cmo.deleteByMeetxh(xh);
			deleteRes = true;
			String topic = mc.getStr("topic");
			String chr = mc.get("chr");
			// + "meetingorder?roomname="+room;//这里的路径据说他们不是 别
			String msg = topic + "会议取消:";
			// sendTextMssage(chr.replace(",", "|"),msg);
			WeChatFactory wcf = new WeChatFactory(chr.replace(",", "|"), msg, PropKit.get(ConfigEnum.HY.getAgentId()));
			Sender pro = wcf.produce();
			pro.Send();
			// SendMsg sm=new SendMsg(chr.replace(",", "|"),msg);
			// sm.start();
		} catch (Exception e) {
			// TODO: handle exception
			throw new RuntimeException(e);
		}
		return deleteRes;
	}

	public boolean search(String room, String starttime, String finishtime) {
		// TODO Auto-generated method stub
		boolean res=false;		
		List<MeetCreate> fb = MeetCreate.me.findByroomAndParam(room,starttime,finishtime);
		if (fb.size()>0) {
			res=true;
		}
		return res;
	}
	
	public boolean search(String xh,String room, String starttime, String finishtime) {
		// TODO Auto-generated method stub
		boolean res=false;		
		List<MeetCreate> fb = MeetCreate.me.findByroomAndParam(xh,room,starttime,finishtime);
		if (fb.size()>0) {
			res=true;
		}
		return res;
	}


	public boolean update(String xh, String room, String topic, String starttime, String finishtime, String zcr,
			String chr, String prechr, String bz, String ydr, String device, String isprivate, String isremind,
			String isdrink, String url, String meis, String naik, String kaib, String nat, String xiangc, String zheng,
			String jiaot, String mok, String guoz) {
		boolean flag = false;
		MeetCreate mc = new MeetCreate().findById(xh);
		try {
			mc.set("room", room);
			mc.set("topic", topic);
			mc.set("starttime", starttime);
			mc.set("finishtime", finishtime);
			mc.set("zcr", zcr);
			mc.set("chr", chr);
			mc.set("prechr", prechr);
			mc.set("meetbz", bz);
			mc.set("ydr", ydr);
			mc.set("device", device);
			mc.set("issend", 0);
			mc.set("status", 0);
			mc.set("isprivate", isprivate);
			mc.set("isremind", isremind);
			mc.update();
			// 发送信息
			// + "meetingorder?roomname="+room;//这里的路径据说他们不是 别

			// + "meetingorder?roomname="+room;//这里的路径据说他们不是 别
			url=url+"&xh="+mc.get("xh");
			String msg = "会议修改:将于" + starttime + "开始,会议主题" + topic + ",<a href=\"" + url + "\">查看</a>";
			// sendTextMssage(chr.replace(",", "|"),msg);
			WeChatFactory wcf = new WeChatFactory(chr.replace(",", "|"), msg, PropKit.get(ConfigEnum.HY.getAgentId()));
			Sender pro = wcf.produce();
			pro.Send();
			//线程终止
			List<CoMeetOrder> li= CoMeetOrder.me.findByMeetXh(xh);
			if (li.size()>0) {
				drinkUpdate(li.get(0), meis, naik, kaib, nat, xiangc, zheng, jiaot, mok, guoz);
			}else{
				drinkSave(xh, meis, naik, kaib, nat, xiangc, zheng, jiaot, mok, guoz);
			}
			flag = true;
		} catch (Exception e) {
			// TODO: handle exception
			throw new RuntimeException(e);
		}
		return flag;
	}


	private void drinkUpdate(CoMeetOrder cmo, String meis, String naik, String kaib, String nat, String xiangc, String zheng,
			String jiaot, String mok, String guoz) {
		// TODO Auto-generated method stub
		cmo.set("meis", meis);
		cmo.set("naik",naik);
		cmo.set("kaib",kaib);
		cmo.set("nat",nat);
		cmo.set("xiangc",xiangc);
		cmo.set("zheng",zheng);
		cmo.set("jiaot",jiaot);
		cmo.set("mok", mok);
		cmo.set("guoz",guoz);		
		cmo.update();
	}




//	public List<MeetCreate> findByRelation(String userid) {
//		// TODO Auto-generated method stub
//		return MeetCreate.me.findByRelation(userid);
//	}
}
